package net.butfly.albacore.utils;

import net.butfly.albacore.exception.SystemException;
import net.butfly.albacore.support.EnumSupport;

public class EnumUtils {

	private EnumUtils() {
	}

	@SuppressWarnings("unchecked")
	public static <E extends EnumSupport<?>> E valueOf(Class<E> clazz, int value) {
		try {
			E[] values = (E[]) clazz.getMethod("values").invoke(clazz);
			for (E v : values) {
				if (v.value() == value) {
					return v;
				}
			}
			return null;
		} catch (Exception e) {
			throw new SystemException("SYS_101", "Parameter should be EnumSupport class.", e);
		}
	}

	@SuppressWarnings("unchecked")
	public static <E extends EnumSupport<?>> E[] values(Class<E> clazz) {
		try {
			return (E[]) clazz.getMethod("values", new Class<?>[0]).invoke(null, new Object[0]);
		} catch (Exception e) {
			throw new SystemException("SYS_101", "Parameter should be EnumSupport class.", e);
		}
	}
}
